package com.fitzone.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

//swagger2配置类
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean
    public Docket adminApiConfig() {
        //添加token参数
        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenParam());

        ApiInfo adminApiInfo = new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("FitZone", "http://atguigu.com", "atguigu@qq.com"))
                .build();

        Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo)
                .select()
                //只显示admin路径下的页面
                .apis(RequestHandlerSelectors.basePackage("com.fitzone.web.controller.admin"))
                .paths(PathSelectors.regex("/admin/.*"))
                .build()
                .globalOperationParameters(pars);
        return adminApi;
    }

    @Bean
    public Docket userApiConfig() {
        //添加token参数
        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenParam());

        ApiInfo miniApiInfo = new ApiInfoBuilder()
                .title("微信小程序-API文档")
                .description("本文档描述了微信小程序接口定义")
                .version("1.0")
                .contact(new Contact("FitZone", "http://atguigu.com", "atguigu@qq.com"))
                .build();

        Docket miniApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("miniApi")
                .apiInfo(miniApiInfo)
                .select()
                //只显示mini路径下的页面
                .apis(RequestHandlerSelectors.basePackage("com.fitzone.web.controller.mini"))
                .paths(PathSelectors.regex("/mini/.*"))
                .build()
                .globalOperationParameters(pars);
        return miniApi;
    }

    private Parameter tokenParam() {
        return new ParameterBuilder()
                .name("token")
                .description("用户token")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
    }

}
